package com.yanqu.road.server.cmd.activity.hanghai.cross;

import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.activity.hanghai.config.HangHaiShipConfig;
import com.yanqu.road.pb.activity.HangHaiActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_HANGHAI_ACTIVITY_GET_UPGRADE_SHIP_LEVEL_FROM_CROSS, desc = "（跨服返回）获取要升级的船的等级")
public class HangHaiGetUpgradeShipLevelFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HangHaiConfig config = HangHaiMgr.getHangHaiConfig();
        if (config == null || !HangHaiMgr.isInOperationTime(config)) {
            ret = GameErrorCode.E_HANGHAI_ACTIVITY_NO_OPEN;
            return;
        }

        HangHaiActivityProto.CrossHangHaiGetUpgradeShipLevelRespMsg reqMsg = HangHaiActivityProto.CrossHangHaiGetUpgradeShipLevelRespMsg.parseFrom(packet.getBytes());
        int shipLevel = reqMsg.getShipLevel();

        HangHaiShipConfig nextLevelConfig = null;
        for (HangHaiShipConfig shipConfig : config.getShipConfigList()) {
            if (shipConfig.getLevel() > shipLevel) {
                nextLevelConfig = shipConfig;
                break;
            }
        }
        if (nextLevelConfig == null) {
            ret = GameErrorCode.E_HANGHAI_ACTIVITY_SHIP_MAX_LEVEL;
            return;
        }

        // 不免费
        String upgradeCost = nextLevelConfig.getUpgradeCost();
        if (!"0".equals(upgradeCost)) {
            // 判断够不够
            if (!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(upgradeCost))) {
                ret = GameErrorCode.E_HANGHAI_ACTIVITY_ITEM_NO_ENOUGH;
                return;
            }
            // 预扣除
            player.getModule(PreReduceModule.class).preReduce(upgradeCost);
        }

        // 发跨服升级
        HangHaiActivityProto.CrossHangHaiUpgradeShipReqMsg.Builder builder = HangHaiActivityProto.CrossHangHaiUpgradeShipReqMsg.newBuilder();
        builder.setActivityId(config.getActivityId());
        builder.setUnionUid(player.getUserInfo().getUnionUid());
        builder.setPreReduceCost(upgradeCost);
        builder.setShipLevel(shipLevel);
        player.sendUnionActivityGroupPacket(CrossProtocol.C2_HANGHAI_ACTIVITY_UPGRADE_SHIP, builder, config.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HangHaiActivityProto.HangHaiUpgradeShipRespMsg.Builder builder = HangHaiActivityProto.HangHaiUpgradeShipRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HANGHAI_ACTIVITY_UPGRADE_SHIP, builder);
        }
    }
}
